package com.biz.primus.management.homepage.init;

import com.biz.primus.account.service.AuthInitializeService;
import com.biz.primus.base.enums.CommonStatus;
import com.biz.primus.model.sso.auth.vo.GrantedAuthority;
import com.biz.primus.model.sso.auth.vo.MenuGroupVO;
import com.biz.primus.model.sso.auth.vo.MenuItemVO;
import com.biz.primus.model.sso.auth.vo.ResourceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

/**
 * Created by defei on 9/19/17.
 */
@Service
public class InitManager {

    private final AuthInitializeService authInitializeService;

    @Autowired
    public InitManager(AuthInitializeService authInitializeService) {
        this.authInitializeService = authInitializeService;
    }

    @PostConstruct
    public void setup() {

        List<MenuGroupVO> menuGroups = newArrayList();
        MenuGroupVO menuGroupVO = new MenuGroupVO("MANAGEMENT_HOMEPAGE", "首页管理", "首页管理", "fa fa-cogs", CommonStatus.ENABLE, 999);
        List<MenuItemVO> menuItems = newArrayList();
        MenuItemVO menuManageMenuItemVO = new MenuItemVO("MANAGEMENT_HOMEPAGE_HOMEPAGE", "首页列表", "显示首页列表", "fa fa-list", "management/homepage", CommonStatus.ENABLE, 1);
        menuManageMenuItemVO.setGrantedAuthority(new GrantedAuthority("ROLE_HOMEPAGE;OPT_HOMEPAGE_LIST"));
        List<ResourceVO> resources = newArrayList();
        resources.add(new ResourceVO("MANAGEMENT_HOMEPAGE_HOMEPAGE_ADD", "新建", "新建首页", CommonStatus.ENABLE, new GrantedAuthority("OPT_HOMEPAGE_CREATE"), 1));
        resources.add(new ResourceVO("MANAGEMENT_HOMEPAGE_HOMEPAGE_UPDATE", "克隆", "克隆首页", CommonStatus.ENABLE, new GrantedAuthority("OPT_HOMEPAGE_CLONE"), 2));
        resources.add(new ResourceVO("MANAGEMENT_HOMEPAGE_HOMEPAGE_EDIT", "编辑", "编辑首页", CommonStatus.ENABLE, new GrantedAuthority("OPT_HOMEPAGE_EDIT"), 3));
        resources.add(new ResourceVO("MANAGEMENT_HOMEPAGE_HOMEPAGE_DELETE", "删除", "删除首页", CommonStatus.ENABLE, new GrantedAuthority("OPT_HOMEPAGE_DELETE"), 4));
        menuManageMenuItemVO.setResources(resources);
        menuItems.add(menuManageMenuItemVO);

        menuGroupVO.setMenuItems(menuItems);
        menuGroups.add(menuGroupVO);
        authInitializeService.registerMenu(menuGroups);
    }

}
